package com.yzi.utilitys
{
	import flash.geom.Matrix;

	public class TransformUtility
	{
		public function TransformUtility()
		{
			throw new Error("Static Class.");
		}
		
		static public function GetScaleX($matrix:Matrix): Number { return Math.sqrt(Math.pow($matrix.a, 2) + Math.pow($matrix.b, 2)); }
		
		static public function SetScaleX($matrix:Matrix, $scaleX:Number): void
		{
			var oldValue:Number = GetScaleX($matrix);
			
			if(oldValue) {
				var ratio:Number = $scaleX / oldValue;
				$matrix.a *= ratio;
				$matrix.b *= ratio;
			} else {
				var skewYRadian:Number = GetSkewYRadians($matrix);
				$matrix.a = (Math.cos(skewYRadian) * $scaleX);
				$matrix.b = (Math.sin(skewYRadian) * $scaleX);
			}
		}
		
		static public function GetScaleY($matrix:Matrix): Number { return Math.sqrt(Math.pow($matrix.c, 2) + Math.pow($matrix.d, 2)); }
		
		static public function SetScaleY($matrix:Matrix, $scaleY:Number): void
		{
			var oldValue:Number = GetScaleY($matrix);
			
			if(oldValue) {
				var ratio:Number = $scaleY / oldValue;
				$matrix.c *= ratio;
				$matrix.d *= ratio;
			} else {
				var skewYRadian:Number = GetSkewXRadians($matrix);
				$matrix.c = (Math.sin(skewYRadian) * $scaleY) * -1;
				$matrix.d = (Math.cos(skewYRadian) * $scaleY);
			}
		}
		
		static public function GetSkewXRadians($matrix:Matrix): Number { return Math.atan2($matrix.c * -1, $matrix.d); }
		
		static public function SetSkewXRadians($matrix:Matrix, $skewX:Number): void
		{
			var scaleY:Number = GetScaleY($matrix);
			$matrix.c = (scaleY * Math.sin($skewX)) * -1;
			$matrix.d = (scaleY * Math.cos($skewX));
		}
		
		static public function GetSkewYRadians($matrix:Matrix): Number { return Math.atan2($matrix.b, $matrix.a); }
		
		static public function SetSkewYRadians($matrix:Matrix, $skewY:Number):void
		{
			var scaleX:Number = GetScaleX($matrix);
			$matrix.a = (scaleX * Math.cos($skewY));
			$matrix.b = (scaleX * Math.sin($skewY));
		}
		
		static public function GetRotationRadians($matrix:Matrix): Number { return GetSkewYRadians($matrix); }
		
		static public function SetRotationRadians($matrix:Matrix, $rotation:Number): void
		{
			var oldRotation:Number = GetRotationRadians($matrix);
			var oldSkewX:Number = GetSkewXRadians($matrix);
			
			SetSkewXRadians($matrix, oldSkewX + $rotation - oldRotation);
			SetSkewYRadians($matrix, $rotation);
		}
		
		static public function GetRotation($matrix:Matrix): Number { return GetRotationRadians($matrix) * (180 / Math.PI); }
		
		static public function SetRotation($matrix:Matrix, $rotation:Number): void { SetRotationRadians($matrix, $rotation * (Math.PI / 180)); }
	}
}